package com.onluy.frameworkcloud.data.util;


class Anchor
{
  public static int[] mark(IDataset dataset, String fix, int fixType)
  {
    int[] marks = new int[dataset.size() + 1];
    int idx = 0;

    if (fixType == 2) {
      String preValue = null;
      String curValue = null;
      for (int i = 0; i < dataset.size(); i++) {
        IData data = (IData)dataset.get(i);
        curValue = data.getString(fix);
        if (!curValue.equals(preValue)) {
          marks[(idx++)] = i;
        }
        preValue = curValue;
      }
    } else if (fixType == 3) {
      int preValue = -2147483648;
      int curValue = -2147483648;
      for (int i = 0; i < dataset.size(); i++) {
        IData data = (IData)dataset.get(i);
        curValue = data.getInt(fix);
        if (curValue != preValue) {
          marks[(idx++)] = i;
        }
        preValue = curValue;
      }
    } else if (fixType == 4) {
      double preValue = (0.0D / 0.0D);
      double curValue = (0.0D / 0.0D);
      for (int i = 0; i < dataset.size(); i++) {
        IData data = (IData)dataset.get(i);
        curValue = data.getDouble(fix);
        if (curValue != preValue) {
          marks[(idx++)] = i;
        }
        preValue = curValue;
      }
    }
    marks[idx] = dataset.size();
    return trimRight(marks);
  }

  private static int[] trimRight(int[] marks) {
    int tail = -1;
    for (int i = marks.length - 1; i >= 0; i--) {
      if (marks[i] != 0) {
        tail = i;
        break;
      }
    }

    int[] ms = new int[tail + 1];

    for (int i = 0; i < ms.length; i++) {
      ms[i] = marks[i];
    }

    return ms;
  }
}